package utils
{
	public class ClusterHelper
	{
		public function ClusterHelper(_positionArr:Array)
		{
			positionArr = _positionArr;
		}
		
		
		private var positionArr:Array;
		/**
		 *定义cluster的左边界 
		 */		
		public var _left:int;
		
		public function get left():int
		{
			var min:int = int.MAX_VALUE;
			
			for(var i:int = 0;i<positionArr.length;i++)
			{
				var arr:Array = positionArr[i];
				
				if(findLeftToRight(arr) < min)
					min = findLeftToRight(arr);
			}
			
			_left = min;
			
			return _left;
		}
		/**
		 *查找左边界 
		 * @param arr
		 * @return 
		 * 
		 */		
		private function findLeftToRight(arr:Array):int
		{
			var min:int = int.MAX_VALUE;
			for(var i:int = 0;i<positionArr.length;i++)
			{
				if(arr[i]== 1)
				{ 
					return i;
				}
			}
			
			return min;
		}
		/**
		 *定义cluster的右边界  
		 */		
		public var _right:int;
		
		public function get right():int
		{
			var max:int = int.MIN_VALUE;
			
			for(var i:int = 0;i<positionArr.length;i++)
			{
				var arr:Array = positionArr[i];				
				if(findRightToLeft(arr) > max)
				{
					max = findRightToLeft(arr);				
				}
			}			
			_right = max;			
			return _right;
		}
		/**
		 *查找右边界 
		 * @param arr
		 * @return 
		 * 
		 */		
		private function findRightToLeft(arr:Array):int
		{
			var max:int = int.MIN_VALUE;
			
			for(var i:int = arr.length-1;i >= 0;i--)
			{
				if(arr[i] == 1)
				{
					
					return i;
				}
			}
			return max;
		}
		/**
		 *定义cluster的上边界  
		 */
		public var _top:int;
		
		public function get top():int
		{
			var min:int = int.MAX_VALUE;
			for(var i:int = 0;i<positionArr.length;i++)
			{				
				if(findNumTopToBottom(i)<min)
				{
					min = findNumTopToBottom(i);
					return min;
				}
			}
			
			_top = min;
			return _top;
		}
		/**
		 *查找上边界 
		 * @param row
		 * @return 
		 * 
		 */		
		private function findNumTopToBottom(row:int):int
		{
			var min:int = int.MAX_VALUE;
			var arr:Array = positionArr[row]; 
			for (var i:int = 0; i < 4; i++) 
			{
				if(arr[i]== 1)
				{
					min = row;
					return min;
				}
			}
			
			return min;
		}
		/**
		 *下边界 
		 */		
		public var _bottom:int;
		
		public function get bottom():int
		{
			var max:int = int.MIN_VALUE;
			for (var i:int = positionArr.length-1; i>0; i--) 
			{
				if(findNumBottomToTop(i)>max)
				{
					max = findNumBottomToTop(i);
					return max;
				}
			}
			_bottom = max;
			return _bottom;
		}
		/**
		 *查找下边界 
		 * @param row
		 * @return 
		 * 
		 */		
		private function findNumBottomToTop(row:int):int
		{
			var arr:Array = positionArr[row];
			for (var i:int = 0; i < 4; i++) 
			{
				if(arr[i]== 1)
				{
					return row;
				}
			}
			return int.MIN_VALUE;
		}
		

	}
}